package com.toobigtofail
{
	/**
	 * TODO:
	 * save serialized level to file.
	 * allow adding of go-force to enemies
	 * allow drawing of build area(s)
	 * allow specification of draw power
	 * allow drawing of lines
	 * allow resizing of objects??
	 **/
	
	import Box2D.Dynamics.b2Body;
	
	import com.toobigtofail.entities.*;
	import com.toobigtofail.images.ShapeImage;
	import com.toobigtofail.palette.Tray;
	import com.toobigtofail.placeholders.Placeholder;
	import com.toobigtofail.shapes.Ball;
	import com.toobigtofail.shapes.Box;
	import com.toobigtofail.shapes.IShape;
	import com.toobigtofail.shapes.Line;
	import com.toobigtofail.shapes.Octagon;
	import com.toobigtofail.utility.Level;
	
	import fabulaexmachina.box2fp.Box2DEntity;
	import fabulaexmachina.box2fp.Box2DWorld;
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	public class LevelEditor extends Box2DWorld
	{
		private var _tray:Tray;
		private var _placeholder:Placeholder;
		
		public function LevelEditor()
		{
			super();
		}
		
		override public function begin():void {
			super.begin();

			// Outer walls
			add(new Box(FP.screen.width/2, FP.screen.height, FP.screen.width/26, 1.0/30));
			add(new Box(FP.screen.width/2, 0, FP.screen.width/26, 1.0/30));
			add(new Box(0, FP.screen.height/2, 1/30, FP.screen.height/26));
			add(new Box(FP.screen.width, FP.screen.height/2, 1/30, FP.screen.height/26, 0, 0, 0, 0, 1, 1, 0.3));
			
			_tray = new Tray(true);
			add(_tray);
			
			this.unpause();
			doDebug();
		}
		
		override public function update():void {
			super.update();
			
			if (_placeholder) {
				_placeholder.centerOnMouse(Input.mouseX,Input.mouseY);
			}
			
			if (Input.mousePressed) {
				if (_placeholder) {
					add(new Obstacle(_placeholder.getShape(), -_placeholder.angle, _placeholder.getImage()));
					remove(_placeholder);
					_placeholder = null;
				}
				else if (_tray.click(Input.mouseX, Input.mouseY)) {
					if (_tray.hasSelectedItem()) {
						_placeholder = _tray.selectedItem;
						add(_placeholder);
					}
				}
				else {
					// TEST - click on object and make it go away
					var entities:Array = new Array;
					getClass(Enemy, entities);
					getClass(Target, entities);
					getClass(BuildingBlock, entities);
					getClass(Obstacle, entities);
					
					for each (var e:Box2DEntity in entities) {
						if (e.collidePoint(e.x, e.y, Input.mouseX, Input.mouseY)) {
							var shape:IShape;
							var angle:Number;
							var image:ShapeImage;
							if (e is Obstacle) {
								var o:Obstacle = e as Obstacle;
								shape = o.shape;
								shape.b2Type = b2Body.b2_staticBody;
								angle = o.angle;
								image = o.image;
								e.remove();
								add(new Enemy(shape, null, angle, image));
							}
							else if (e is Enemy) {
								var en:Enemy = e as Enemy;
								shape = en.shape;
								shape.b2Type = b2Body.b2_staticBody;
								angle = en.angle;
								image = en.image;
								e.remove();
								add(new Target(shape, angle, image));
							}
							else if (e is Target) {
								e.remove();
							}
							break;
						}
					}
				}
			}
			
			// Save the level
			if (Input.pressed(Key.SPACE)) {
				saveLevel();
			}
		}
		
		private function saveLevel():void {
			var level:Level = new Level(null, true);
			
			var entities:Array = new Array;
			getClass(Enemy, entities);
			getClass(Target, entities);
			getClass(BuildingBlock, entities);
			getClass(Obstacle, entities);
			
			for each (var e:* in entities) {
				level.addEntity(e);
			}
			
			var placeholders:Array = _tray.getAllPlaceholders();
			for each (var p:Placeholder in placeholders) {
				level.addPlaceholder(p);
			}
			
			trace(level.serialize());
		}
	}
}